package com.example.newproject_overseas.components

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.newproject_overseas.model.entity.Category

@Composable
fun CategoryTabs(
    categories: List<Category>,
    selectIndex: Int,
    onTabSelected: (Int) -> Unit,
    content: @Composable (index:Int,category:Category) -> Unit
) {
    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.Start,
        modifier = Modifier
            .fillMaxWidth()
            .height(60.dp)
            .background(Color(0xFFFFF6D4))
    ) {
        Row {
            categories.forEachIndexed { index, category ->
                Column {
                    Text(
                        text = category.title,
                        fontSize = 15.sp,
                        color = if (index == selectIndex) Color(0xFFFFCC00) else Color.Black,
                        modifier = Modifier
                            .padding(start = 20.dp)
                            .clickable(
                                interactionSource = remember { MutableInteractionSource() },
                                indication = null
                            ) {

                                onTabSelected(index)
                            }
                    )

                    if(index == selectIndex){
                        SmaleHorizontalLine(category.title)
                    }

                }
            }
        }
        
    }

    Column() {
        categories.forEachIndexed { index, category ->
            if(index == selectIndex){
                content(index,category)

            }

        }
    }
   
}




@Composable
fun SmaleHorizontalLine(title: String) {
    val titleCount = title.count()
    Canvas(
        modifier = Modifier
            .width((titleCount * 11).dp)
            .padding(top = 5.dp)
            .padding(start = 22.dp)
            .height(2.dp)
    ) {
        drawLine(
            color = Color(0xFFFFCC00),
            start = Offset(0f, 0f),
            end = Offset(size.width, 0f),
            strokeWidth = 4.dp.toPx(),
            cap = StrokeCap.Round
        )
    }
}